package com.wang.tool.executor.transitivethreadcontext;


import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * 自切换ExecutorService
 *
 * @Author shaopeng
 * @Date 2021/11/13
 */
public class TransitiveExecutorService implements ExecutorService {
    /**
     * 被代理 非空
     */
    private final ExecutorService delegate;


    /**
     * @param delegate 被代理
     */
    public TransitiveExecutorService(ExecutorService delegate) {
        if (delegate == null) {
            throw new IllegalArgumentException("delegateExecutorService can not be null");
        }
        this.delegate = delegate;
    }


    @Override
    public void shutdown() {
        delegate.shutdown();
    }


    @Override
    public List<Runnable> shutdownNow() {
        return delegate.shutdownNow();
    }

    @Override
    public boolean isShutdown() {
        return delegate.isShutdown();
    }

    @Override
    public boolean isTerminated() {
        return delegate.isTerminated();
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return delegate.awaitTermination(timeout, unit);
    }


    @Override
    public void execute(Runnable command) {
        delegate.execute(ThreadContextTransferTool.wrap(command));
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return delegate.submit(ThreadContextTransferTool.wrap(task));
    }


    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return delegate.submit(ThreadContextTransferTool.wrap(task), result);
    }


    @Override
    public Future<?> submit(Runnable task) {
        return delegate.submit(ThreadContextTransferTool.wrap(task));
    }


    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        return delegate.invokeAll(ThreadContextTransferTool.wrapCalls(tasks));
    }


    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        return delegate.invokeAll(ThreadContextTransferTool.wrapCalls(tasks), timeout, unit);
    }


    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        return delegate.invokeAny(ThreadContextTransferTool.wrapCalls(tasks));
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return delegate.invokeAny(ThreadContextTransferTool.wrapCalls(tasks), timeout, unit);
    }


    public ExecutorService getDelegate() {
        return delegate;
    }

    @Override
    public boolean equals(Object o) {
        return delegate.equals(o);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName() + "[" + delegate + "]";
    }

}
